package com.joysuch.wwyt.bp.repository;

import com.joysuch.wwyt.bp.entity.investigate.BpInvestigateRecordItem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @Author ZhangDong
 * @Date 2020/12/8 15:19
 */
public interface BpInvestigateRecordItemDao extends JpaRepository<BpInvestigateRecordItem, Long> {

    List<BpInvestigateRecordItem> findByRecordId(Long recordId);

    BpInvestigateRecordItem findFirstByRecordId(Long recordId);

    @Query("update BpInvestigateRecordItem i set i.deleteFlag='1' where i.recordId=:recordId")
    @Modifying
    void deleteByRecordId(Long recordId);

    @Query("update BpInvestigateRecordItem i set i.deleteFlag='1' where i.recordId in :ids")
    @Modifying
    void deleteByRecordIds(@Param("ids") List<Long> ids);

    @Query(value = "SELECT DISTINCT(detail.item_id) FROM `wwyt_risk_point_business_evaluation_detail` detail " +
            "INNER JOIN `wwyt_risk_hidden_danger_content` content ON detail.id = content.detail_id " +
            "INNER JOIN `wwyt_bp_investigate_plan` plan ON content.id = plan.hidden_danger_content_id " +
            "INNER JOIN `wwyt_bp_investigate_record` record ON plan.id = record.`check_plan_id` " +
            "INNER JOIN `wwyt_bp_investigate_record_item` item ON item.`record_id` = record.`id` " +
            "WHERE detail.delete_flag = '0' AND item.id =?1",nativeQuery = true)
    Long getPointEvalItemIdById(Long recordItemId);

    @Query(value = "SELECT DISTINCT(detail.item_id) FROM `wwyt_risk_point_business_evaluation_detail` detail " +
            "INNER JOIN `wwyt_risk_hidden_danger_content` content ON detail.id = content.detail_id " +
            "INNER JOIN `wwyt_bp_investigate_plan` plan ON content.id = plan.hidden_danger_content_id " +
            "INNER JOIN `wwyt_bp_investigate_record_his` record ON plan.id = record.`check_plan_id` " +
            "INNER JOIN `wwyt_bp_investigate_record_item_his` item ON item.`record_id` = record.`id` " +
            "WHERE detail.delete_flag = '0' AND item.id =?1",nativeQuery = true)
    Long getPointEvalItemIdByHistoryId(Long recordItemId);

    List<BpInvestigateRecordItem> findByRecordIdIn(List<Long> recordIds);
}
